//
//  WBComposeView.swift
//  SinaWeibo
//
//  Created by zhuangqianwei on 16/11/20.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

import UIKit
import SnapKit
import pop

class WBComposeView: UIView {

    
    lazy var buttonArray: [WBComposeBtn] = [WBComposeBtn]()
    //展现撰写视图
    func show(fromVC: UIViewController?) {
        //将当前的视图添加到window上
        fromVC?.view .addSubview(self)
        
        for (index,btn) in buttonArray.enumerated(){
            
            self.starAnimation(btn: btn, Index: index)
            
        }

        
    }
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        self.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
        setupUI()
        addComposeBtns()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //开始动画
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        
    }
    //开始撰写按钮的动画效果
    private func starAnimation(btn: WBComposeBtn, Index: Int, isShow: Bool = true){
        
        //添加动画对象
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        //attributte
        springAnimation?.springSpeed = 10.0
        springAnimation?.springBounciness = 10.0
        //dispatchtime
        springAnimation?.beginTime = CACurrentMediaTime() + Double(Index) * 0.025
        //三目
        let btnCenterY = btn.center.y + CGFloat(isShow ? -350: 350)
        springAnimation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btnCenterY))
        btn.pop_add(springAnimation, forKey: nil)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for (index,btn) in buttonArray.reversed().enumerated() {
            
            self.starAnimation(btn: btn, Index: index,isShow: false)
            
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
            self.removeFromSuperview()
        }
        
    }
    
 
    //按钮的放大和缩小动画
    @objc private func composeBtnClick(btn: WBComposeBtn) {
        //被点击的安宁执行放大，其他缩小  
        UIView.animate(withDuration: 0.5, animations: {
            //遍历数组 获取按钮
            for composeBtn in  self.buttonArray {
                if btn == composeBtn {
                    //被点击的安宁 放大
                    composeBtn.transform = CGAffineTransform.init(scaleX: 1.5, y: 1.5)
                }else{
                    composeBtn.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
                    //修改alpha
                    composeBtn.alpha = 0.1
                }
             
            }
            
        }
            , completion: { (_) -> () in
            //跳转对应的控制器,通过网络数据分析了一个控制器的字符串
                let bundleName = Bundle.main.infoDictionary!["CFBundleName"] as! String

                let className = bundleName + "." + "WBComposeController"
                
                //获取类型
                guard let classType = NSClassFromString(className) as? UIViewController.Type
                    else{
                        return
                }
                //根据类型实例化控制器对象
                let vc = classType.init()
                let nav = UINavigationController(rootViewController: vc)
                
                UIApplication.shared.keyWindow?.rootViewController?.present(nav, animated: true, completion: {
                    //跳转完控制器之后 希望立即执行一些操作的时候
                  self.removeFromSuperview()
                })
        })
        
        
        
        
        
        
    }
    //添加撰写按钮
    private func addComposeBtns() {
        
        let btnMargin = (ScreenWidth - 3 * composeBtnImageWidth) / 4
        
        for i in 0..<6 {
            
            let btn = WBComposeBtn()
            //应该确定对应的列和行的索引
            let colIndex = i % 3
            let rowIndex = i / 3
            //计算x
            let btnX = btnMargin + (btnMargin + composeBtnImageWidth) * CGFloat(colIndex)
            let btnY = (btnMargin + composeBtnHeight) * CGFloat(rowIndex) + ScreenHeight
            btn.setImage(#imageLiteral(resourceName: "tabbar_compose_lbs"), for: UIControlState.normal)
            btn.frame = CGRect(x: btnX, y: btnY, width: composeBtnImageWidth, height: composeBtnHeight)
            btn.setTitle("@风神", for: UIControlState.normal)
            
            self.addSubview(btn)
            buttonArray.append(btn)
            //监听按钮的点击
            btn.addTarget(self, action: #selector(composeBtnClick(btn:)), for: UIControlEvents.touchUpInside)
        }
        
    }
    private func setupUI() {
        //添加被处理毛玻璃效果的背景视图  截取屏幕
//        let toolBar = UIToolbar(frame: UIScreen.main.bounds)
//        toolBar.barStyle = .black
        self.addSubview(backView)
//        self.addSubview(toolBar)
        self.addSubview(sloganView)
        
        
        
        sloganView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        
    }
    
    private lazy var backView: UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow().applyLightEffect())
    private lazy var sloganView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
    
}
